package com.yaojiaxun.logisticsPlatformSystem.controller;

import com.yaojiaxun.logisticsPlatformSystem.dto.*;
import com.yaojiaxun.logisticsPlatformSystem.entity.ClientInfo;
import com.yaojiaxun.logisticsPlatformSystem.entity.RouteInfo;
import com.yaojiaxun.logisticsPlatformSystem.service.*;
import com.yaojiaxun.logisticsPlatformSystem.util.MD5Util;
import com.yaojiaxun.logisticsPlatformSystem.util.ResultVOUtil;
import com.yaojiaxun.logisticsPlatformSystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class FrontController {

    @Autowired
    private RouteInfoService routeInfoService;

    @Autowired
    private MaterialInfoService materialInfoService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private PersonInformationService personInformationService;

    @Autowired
    private ClientInfoService clientInfoService;

    @Autowired
    private NewsService newsService;

    @RequestMapping("/")
    public String hello(HttpSession session){
        session.setAttribute("user", null);
        return "redirect:/logisticsPlatform";
    }

    @RequestMapping("/front_logout")
    public String frontLogOut(HttpSession session){
        session.setAttribute("user", null);
        return "redirect:/logisticsPlatform/logIn";
    }

    //登陆前
    @RequestMapping("/logisticsPlatform/logIn")
    public String frontLogIn(HttpSession session, Map<String, Object> map){
        session.setAttribute("user", null);
        return "front/log/log_login";
    }

    //登录时
//    @RequestMapping("/logisticsPlatform/logInAfter")
//    public String frontLogInAfter(@RequestParam("account") String account, @RequestParam("password") String password, HttpSession session, Map<String, Object> map){
//        if (account.equals(personInformationService.findPersonByPersonAccount(account).getPersonAccount())){
//            if (password.equals(personInformationService.findPersonByPersonAccount(account).getPassword())){
//                session.setAttribute("user", account);
//                map.put("user", personInformationService.findPersonByPersonAccount(account));
//                return "redirect:/logisticsPlatform";
//            }
//        }
//        return "front/log/log_logIn";
//    }
    @RequestMapping("/logisticsPlatform/logInAfter")
    public String frontLogInAfter(ClientInfo clientInfo, HttpSession session, Map<String, Object> map){
        if (clientInfoService.findClientByClientAccount(clientInfo.getClientAccount())!=null){
            if (MD5Util.inputPassToDbPass(clientInfo.getClientPassword()).equals(clientInfoService.findClientByClientAccount(clientInfo.getClientAccount()).getClientPassword())){
                session.setAttribute("user", clientInfo.getClientAccount());
                return "redirect:/logisticsPlatform";
            }
            else return "front/log/log_passError";
        }
        return "front/log/log_logIn";
    }
//    public String frontLogInAfter(@RequestParam("account") String account, @RequestParam("password") String password, HttpSession session, Map<String, Object> map){
//        if (account.equals(clientInfoService.findClientByClientAccount(account).getClientAccount())){
////            if (password.equals(clientInfoService.findClientByClientAccount(account).getClientPassword())){
////                session.setAttribute("user", account);
////                map.put("user", clientInfoService.findClientByClientAccount(account));
////                return "redirect:/logisticsPlatform";
////            }
//            if (MD5Util.inputPassToDbPass(password).equals(clientInfoService.findClientByClientAccount(account).getClientPassword())){
//                session.setAttribute("user", account);
//                map.put("user", clientInfoService.findClientByClientAccount(account));
//                return "redirect:/logisticsPlatform";
//            }
//            else
//                return "front/log/log_passError";
//        }
//        return "front/log/log_logIn";
//    }

    @RequestMapping("/logisticsPlatform")
    public String frontIndex(Map<String, Object> map, HttpSession session){
        map.put("routes", routeInfoService.findLast6Route());
        map.put("materials", materialInfoService.findLast6Material());
        map.put("companies", companyInfoService.findLast6Company());
        map.put("newsS", newsService.findLast6News());
        map.put("newsSOther", newsService.findOther6News());
        return "front/log/front_index";
    }

    @RequestMapping("/logisticsPlatform/material_detail_front")
    public String getMaterialDetailFront(@RequestParam("materialId") Long materialId, Map<String, Object> map){
        map.put("materialDetail", materialInfoService.findMaterialByIdFormatTime(materialId));
        map.put("comIntro", companyInfoService.findCompanyByName(materialInfoService.findMaterialById(materialId).getCompany()).getIntroduction());
        return "front/material/material_detail_front";
    }

    @RequestMapping("/logisticsPlatform/route_detail_front")
    public String getRouteDetailFront(@RequestParam("routeId") Long routeId, Map<String, Object> map){
        map.put("route", routeInfoService.findRouteInfoTimeFormat(routeId));
        map.put("routeCom", companyInfoService.findCompanyByName(routeInfoService.findRouteInfo(routeId).getRouteCompany()).getIntroduction());
        return "front/route/route_detail_front";
    }

    @RequestMapping("/logisticsPlatform/news_detail_front")
    public String getNewsDetailFront(@RequestParam("newsId") Long newsId, Map<String, Object> map){
        map.put("news", newsService.findNewsInfoTimeFormat(newsId));
        map.put("newsSide", newsService.findLast6News());
        return "front/news/news_detail_front";
    }
    @ResponseBody
    @RequestMapping("/getNewsContent")
    public ResultVO getNewsDetailFront1(@RequestParam("newsId") Long newsId){
        return ResultVOUtil.success(newsService.findNewsInfoTimeFormat(newsId));
    }

    @RequestMapping("/logisticsPlatform/route_list_all")
    public String RouteListAll(@RequestParam(defaultValue = "1") Integer page, Map<String, Object> map){
        PageDTO<RouteInfoDTO> pageDTO = routeInfoService.findPage(page);
        map.put("pageDTO", pageDTO);
        return "front/route/route_list_all";
    }

    @RequestMapping("/logisticsPlatform/material_list_all")
    public String MaterialListAll(@RequestParam(defaultValue = "1") Integer page, Map<String,Object> map){
        PageDTO<MaterialInfoDTO> pageDTO = materialInfoService.findPage(page);
        map.put("pageDTO", pageDTO);
        return "front/material/material_list_all";
    }

    @RequestMapping("/logisticsPlatform/company_list_all")
    public String CompanyListAll(@RequestParam(defaultValue = "1") Integer page, Map<String, Object> map){
        PageDTO<CompanyInfoDTO> pageDTO = companyInfoService.findPage(page);
        map.put("pageDTO", pageDTO);
        return "front/company/company_list_all";
    }

    @RequestMapping("/logisticsPlatform/news_list_all")
    public String NewsListAll(@RequestParam(defaultValue = "1") Integer page, Map<String, Object> map){
        PageDTO<NewsDTO> pageDTO = newsService.findPage(page);
        map.put("pageDTO", pageDTO);
        return "front/news/news_list_all";
    }
}
